﻿/*
Usage
var _Clip:preload = new preload();
_Clip.addEventListener(LoadEvent.ONLOAD, handleOnload);
_Clip.init({url:"someFlash.swf", anime: tools.searchLib('lib_loading'), x: 150, y:200 }, true, false);	// _autoLoad, _autoPlay
addChild( _Clip );

Usage2
var _Clip:preload = new preload();
_Clip.init({url:"someFlash.swf"});
_Clip.load();

*/
package com.cf.core.model{
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.display.AVM1Movie;
	import flash.text.TextField;
	import flash.events.EventDispatcher;
	import flash.net.URLRequest;
	import flash.net.LocalConnection;
	import com.cf.core.Utils.*; // 基於 cache 工具包
	import com.cf.core.events.LoadEvent;
	
	import com.cf.core.control.SyncSprite;
	public class preload extends SyncSprite{
		// 目標讀入地址
		private var G_url:String="";
		public function get url():String{return G_url}//fn
		public function set url(_s:String):void{G_url=_s}//fn
		
		// 使用之 Preload 動畫, library name of current scene
		private var G_anime:*=new MovieClip();
		public function get anime():*{return G_anime}//fn
		public function set anime(_o:*):void{G_anime=_o; animeClip()}//fn
		
		private var G_content:*;
		public function get content():*{return G_content}
		public function set setContent(_o:*):void{G_content=_o}
		
		private var G_type:String;
		private var G_control:cache;
		private var G_autoPlay:Boolean;
		protected function set setAutoPlay(_b:Boolean):void{G_autoPlay=_b}
		
		private var G_haveani:Boolean;
		private var G_aniTxt:Boolean;
		private var G_cntTxt:TextField;
		
		public function preload():void{onCreate();unload()}
		public function init(_o:Object, _autoLoad:Boolean=true, _autoPlay:Boolean=true):void{
			// transform the object to standard format
			if(_o.anime != undefined){ this.anime = _o.anime }
			if(_o.x !=undefined){ this.anime.x = _o.x }
			if(_o.y !=undefined){ this.anime.y = _o.y }
			if(_o.url != undefined){ this.url = _o.url }
			if(_autoLoad){
				load();
			}//if
			setContent=new Sprite();
			if(_autoPlay) setAutoPlay=_autoPlay;
		}//fn
		private function animeClip():void{
			// 如果這是可插放MovieClip 0~100 格.
			if( this.anime.totalFrames>=100 ){
				// trace("preload : loading anime exist.");
				this.anime.stop();
				G_haveani=true;
			}//if
			for(var i:int=0;i<this.anime.numChildren;i++){
				if( this.anime.getChildAt(i) is TextField){
					// we search for the 100% conuter textfield...
					// Guess what? we can't control designer makeup the name of the FXXK field.
					// so we just search some textField and rewrite on it. FXXK THIS.
					// trace("preload : Counter textField init");
					G_cntTxt = this.anime.getChildAt(i);
					G_aniTxt = true;
					break;
				}//if
			}//for
		}//fn
		public function onCreate():void{}// override
		public function unload():void{
			G_url = new String;
			G_anime = new MovieClip;
			G_type = new String;
			G_haveani = false;
			G_aniTxt = false;
			G_cntTxt = new TextField;
			setAutoPlay = false;
			setContent = new Sprite;
			try{ G_control.RELEASE(); }catch(e){}
			G_control=new cache();
			tools.emptySprite(this);
		}//fn
		public function load():void{
			// start load url;
			// url detect as "SWF","IMG","HTML"
			addChild(this.anime);
			G_type=tools.filetype(this.url);
			switch( G_type ){
				default:
				case tools.SWF:
					G_control.address("loader", cacheSWF);
					G_control.address("loader").addEventListener(LoadEvent.ONPROGRESS, handleProgress);
					G_control.address("loader").addEventListener(LoadEvent.ONLOAD, handleOnload);
					G_control.address("loader").loadSWF(this.url);
					break;
				case tools.IMG:
					G_control.address("loader", cacheIMG);
					G_control.address("loader").addEventListener(LoadEvent.ONLOAD, handleOnload);
					G_control.address("loader").loadImage(this.url);
					break;
				case tools.HTML:
					// G_control.address("loader", cacheSWF);
					//G_control.address("loader").addEventListener(LoadEvent.ONLOAD, handleOnload);
					break;
			}//case
		}//fn
		
		public function handleOnload(e:LoadEvent):void{
			tools.emptySprite(this);
			switch( G_type ){
				default:
				case tools.SWF:
					if( G_control.address("loader").SWF is AVM1Movie){
						// bug : if loaded swf is AVM1Movie will cause Error.
						// this.addChild(MovieClip); // null it until found the fixup solution
						trace("preload : AVM1MOVIE detected, Please use cacheSWF to repair AVM1's movieclip");
					}else{
						addChild( G_control.address("loader").SWF );
					}//if
					try{
						if(G_autoPlay)	G_control.address("loader").SWF.play();
						else			G_control.address("loader").SWF.stop();
					}catch(e){}
					this.setContent = G_control.address("loader").SWF;
					G_control.address("loader").removeEventListener(LoadEvent.ONLOAD, handleOnload);
					G_control.address("loader").removeEventListener(LoadEvent.ONPROGRESS, handleProgress);
					G_control.address("loader").releaseRAM();
				break;
				case tools.IMG:
					addChild( G_control.address("loader").IMG );
					this.setContent = G_control.address("loader").IMG;
					G_control.address("loader").releaseRAM();
					G_control.address("loader").removeEventListener(LoadEvent.ONLOAD, handleOnload);
				break;
				case tools.HTML:
					// this.setContent = G_control.address("loader");
					G_control.address("loader").releaseRAM();
				break;
			}//case
			dispatchEvent(new LoadEvent(LoadEvent.ONLOAD, {url: G_url, content: this.content }));
		}//fn
		public function handleProgress(e:LoadEvent):void{
			if(e.type == LoadEvent.ONPROGRESS){
				var _pt:int = Math.ceil((e.data.bytesLoaded/e.data.bytesTotal)*100);
				trace("preload : "+_pt+"% - "+ e.data.bytesLoaded +"/"+e.data.bytesTotal+ " bytes ");
				if(G_haveani){
					this.anime.gotoAndStop(_pt);
				}//if
				if(G_aniTxt){
					G_cntTxt.text = tools.format(_pt,3) +"%";
				}//if
			}//if
		}//fn
	}//class
}//package